using System;
using Timer = System.Windows.Forms.Timer;

namespace 第一次作业
{
    public partial class Form1 : Form
    {
        private Random random;
        private int currentQuestionIndex;
        private int totalQuestions;
        private int correctAnswers;
        private Timer timer;

        public Form1()
        {
            InitializeComponent();

            random = new Random();
            currentQuestionIndex = 0;
            totalQuestions = 10;
            correctAnswers = 0;

            // 初始化定时器
            timer = new Timer();
            timer.Interval = 10000;
            timer.Tick += Timer_Tick;
        }

        private void GenerateQuestion()
        {
            int num1 = random.Next(1, 101);
            int num2 = random.Next(1, 101);
            int operation = random.Next(1, 3);
            currentQuestionIndex ++;

            string operatorChar = operation == 1 ? "+" : "-";
            quizArea.Text = $"{num1} {operatorChar} {num2}";
            ansArea.Text = string.Empty;
            quizNum.Text = $"第 {currentQuestionIndex} 题";
            ansArea.BackColor = System.Drawing.Color.White;

            timer.Start();

            this.nextBttn.Enabled = false;
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            timer.Stop();

            CheckAnswer();

            if (currentQuestionIndex < totalQuestions)
            {
                GenerateQuestion();
            }
            else
            {
                ShowResult();
            }
        }

        private void CheckAnswer()
        {
            if (string.IsNullOrWhiteSpace(ansArea.Text))
            {
                return;
            }

            int userAnswer;
            if (!int.TryParse(ansArea.Text, out userAnswer))
            {
                return;
            }

            int expectedAnswer;
            if (quizArea.Text.Contains("+"))
            {
                string[] operands = quizArea.Text.Split('+');
                expectedAnswer = int.Parse(operands[0].Trim()) + int.Parse(operands[1].Trim());
            }
            else if (quizArea.Text.Contains("-"))
            {
                string[] operands = quizArea.Text.Split('-');
                expectedAnswer = int.Parse(operands[0].Trim()) - int.Parse(operands[1].Trim());
            }
            else
            {
                return;
            }

            if (userAnswer == expectedAnswer)
            {
                ansArea.BackColor = System.Drawing.Color.LightGreen;
                correctAnswers++;
            }
            else
            {
                ansArea.BackColor = System.Drawing.Color.LightPink;
            }

            timer.Stop(); // 停止定时器
            if (currentQuestionIndex >= totalQuestions)
            {
                ShowResult();
            }
        }

        private void ShowResult()
        {
            timer.Stop();

            int score = (int)((double)correctAnswers / totalQuestions * 100);
            MessageBox.Show($"答题完成！得分：{score}%");

            currentQuestionIndex = 0;
            correctAnswers = 0;

            this.startBttn.Enabled = true;
        }

        private void startBttn_Click(object sender, EventArgs e)
        {
            this.startBttn.Enabled = false;
            this.ansArea.Enabled = true;
            this.timerLabel.Visible = true;
            this.ansArea.Visible = true;
            this.timerLabel.Visible = true;
            this.quizArea.Visible = true;
            this.equalChara.Visible = true;
            this.yesBttn.Visible = true;
            this.nextBttn.Visible = true;
            this.quizNum.Visible = true;

            GenerateQuestion();

        }

        private void yesBttn_Click(object sender, EventArgs e)
        {
            this.yesBttn.Enabled = false;
            CheckAnswer();
            this.nextBttn.Enabled = true;
            
        }

        private void nextBttn_Click(object sender, EventArgs e)
        {
            GenerateQuestion();
            this.yesBttn.Enabled = true;
            this.nextBttn.Enabled = false;
        }
    }
}